export function suiji(query) {
    let data={
        //获取 0～1.0 之间的随机数：
        a:Math.random(),
        //随机获取 0 和 1
        b:Math.round(Math.random()),
        //随机获取 0～100 之间的一个整数：
        c:Math.round(Math.random()*100),
        //随机获取 10～50 之间的一个整数：
        d:Math.round(Math.random()*10+40),
    };
    return data;
}


//懒加载 js
/**
 * 动态加载js
 * @param url
 * @param callback
 * @returns {Promise<any>}
 */
export function loadingJs(url, callback) {
return new Promise((resolve, reject) => {
    let fn = callback || function () {};
    let script = document.createElement('script');
    script.src = url;
    script.type = 'text/javascript';
    document.head.appendChild(script);
    if (script.addEventListener) {
    script.addEventListener('load', function () {
        console.log("加载完成",url);
        fn();
        resolve();
    }, false);
    } else if (script.attachEvent) {
    script.attachEvent('onreadystatechange', function () {
        let target = window.event.srcElement;
        if (target.readyState == 'loaded') {
        console.log("加载完成",url);
        fn();
        resolve();
        }
    });
    }
})
}

/**
 * 动态加载css
 * @param href
 * @param callback
 * @returns {Promise<any>}
 */
export function loadingCss(href, callback) {
return new Promise((resolve, reject) => {
    let fn = callback || function () {};
    let link = document.createElement('link');
    link.href = href;
    link.rel = 'stylesheet';
    document.head.appendChild(link);
    superviseCssLinkLoad(link, () => {  fn();resolve();console.log("加载完成",href); })
})
}

/**
 * 通过轮询link标签的sheet. cssRules来判断是否加载好css资源
 * @param link
 * @param onload
 */
function superviseCssLinkLoad(link, onload) {
requestIdleCallback(() => {
    if (link.sheet) {
    if (link.sheet.cssRules?.length) {
        typeof onload === 'function' && onload()
    } else {
        superviseCssLinkLoad(link, onload)
    }
    } else {
    superviseCssLinkLoad(link, onload)
    }
})
}